﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Web;

namespace Hsinchu.Utility
{
    /*
     * 错误处理专用类
     */
    public class ErrorHandle
    {
        //定义系统日志文件路径，从mappath获得
        public static string FILE_NAME = HttpContext.Current.Server.MapPath("~/") + "Log/SystemLog.txt";



        /// <summary>
        /// 写入错误日志
        /// </summary>
        /// <author>zsl</author>
        /// <date>2008-12-19</date>
        /// <param name="message">传递的异常信息</param>
        public static void Log(string message)
        {
            if (File.Exists(FILE_NAME))
            {
                StreamWriter sr = new StreamWriter(FILE_NAME, true, System.Text.Encoding.GetEncoding("GB2312"));
                sr.WriteLine("\n");
                sr.WriteLine("***************************************************************");
                sr.WriteLine(DateTime.Now.ToString() + message);
                sr.Close();
            }
            else
            {
                StreamWriter sr = File.CreateText(FILE_NAME);
                sr.Close();
            }
        }

        /// <summary>
        /// 发生非异常类错误重定向页面并显示信息
        /// </summary>
        /// <param name="ErrorMessage">错误信息</param>
        /// <author>grokyao</author>
        /// <date>2008-12-19</date>
        public static void RedirectErrorPage(string _ErrorMessage)
        {
            HttpContext.Current.Response.Redirect("~/ErrorPage.aspx?ErrorMessage=" + _ErrorMessage);
        }
    }
}
